const express = require("express");
const router = express.Router();
const { timeTrans } = require("../../../utils");
const multer = require("multer");
const upload = multer({ dest: "../../../public/images" });
const {
  getClassification,
  showClassification,
  newClassification,
  addClassification,
  updateClassification,
  deleteClassification,
  batchClassification,
} = require("../../../model/weixin/admin/classification");
// 获取
router.post("/", async (req, res) => {
  let data = await getClassification();
  res.send(data);
});
// 展示
router.get("/show", async (req, res) => {
  const isShow = req.query.isShow === "false" ? false : true;
  const classi_id = Number(req.query.classi_id);
  showClassification([isShow, classi_id])
    .then(() => {
      res.send({
        code: 200,
        msg: "修改图片成功",
      });
    })
    .catch((err) => {
      res.send({
        code: 500,
        msg: "修改图片失败",
      });
    });
});
// 显示new
router.get("/new", async (req, res) => {
  const isNew = req.query.isNew === "false" ? false : true;
  const classi_id = Number(req.query.classi_id);
  newClassification([isNew, classi_id])
    .then(() => {
      res.send({
        code: 200,
        msg: "修改图片成功",
      });
    })
    .catch((err) => {
      res.send({
        code: 500,
        msg: "修改图片失败",
      });
    });
});

// 添加轮播图片
router.post("/add", upload.single("classification"), (req, res, next) => {
  const classi_id = Number(req.body.classi_id);
  const src = "http://localhost:3333/images/" + req.body["src[name]"];
  const atitle = req.body.atitle;
  const remaks = req.body.remaks;
  const atime = timeTrans(req.body.atime);
  addClassification([classi_id, src, atime, atitle, remaks])
    .then(() => {
      res.send({
        code: 200,
        msg: "添加轮播图片成功",
      });
    })
    .catch((err) => {
      res.send({
        code: 500,
        msg: "添加轮播图片失败",
        err,
      });
    });
});

// 修改轮播图片
router.post("/update", (req, res, next) => {
  console.log(req.body);
  const classi_id = Number(req.body.classi_id);
  const atitle = req.body.atitle;
  const remaks = req.body.remaks;
  const src =
    "http://localhost:3333/images/" + req.body["src[name]"] ||
    req.body["src[name]"];
  const atime = timeTrans(req.body.atime);
  updateClassification([src, atime, atitle, remaks, classi_id])
    .then(() => {
      res.send({
        code: 200,
        msg: "修改轮播图片成功",
      });
    })
    .catch((err) => {
      res.send({
        code: 500,
        msg: "修改轮播图片失败",
        err,
      });
    });
});

// 删除单个轮播图片
router.get("/delete", (req, res, next) => {
  const classi_id = Number(req.query.classi_id);
  deleteClassification([classi_id])
    .then(() => {
      res.send({
        code: 200,
        msg: "删除图片成功",
      });
    })
    .catch((err) => {
      res.send({
        code: 500,
        msg: "删除图片失败",
      });
    });
});

// 删除多个轮播图片
router.get("/batch", (req, res, next) => {
  console.log(req.query.data);
  batchClassification(req.query.data)
    .then(() => {
      res.send({
        code: 200,
        msg: "删除图片成功",
      });
    })
    .catch((err) => {
      res.send({
        code: 500,
        msg: "删除图片失败",
      });
    });
});
module.exports = router;
